#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int maxn = 120000 + 10;
constexpr int maxm = 20;
struct node
{
    i64 t[maxm], m;
    node() {m = 0; memset(t, 0, sizeof(t));}
};
int b[maxn], c[maxn];
string s[maxm];
int a[maxn];
int n, m, k;
int st[maxm][maxn];
#define get(x, k) ((((x) + (x) + (1 << (k)) - 1ll) << (k)) >> 1)
void ask(int x, int y, int k, node& p)
{
    if (x + (1 << k) - 1 <= y)
    {
        p.t[max(k, a[st[k][x]])] = st[k][x];
        return;
    }
    if (x + (1 << k - 1) > y)
    {
        ask(x, y, k - 1, p);
        i64 r = get(x + (1 << k - 1), k - 1);
        if (s[k][(x - 1) >> k] == '0')
        {
            if (p.m || p.t[k - 1]) p.m += r;
            p.t[k - 1] = 0;
        }
        else
        {
            p.m += r;
            p.t[k] += p.t[k - 1];
            p.t[k - 1] = 0;
        }
        return;
    }
    else
    {
        int l = st[k - 1][x], v = a[l];
        if (s[k][(x - 1) >> k] == '0')
        {
            if (v >= k)
            {
                p.t[max(k, v)] = l;
                return;
            }
            else
            {
                ask(x + (1 << k - 1), y, k - 1, p);
                p.t[k] += p.t[k - 1];
                p.t[k - 1] = 0;
                return;
            }
        }
        else
        {
            ask(x + (1 << k - 1), y, k - 1, p);
            int o = !!(p.m | p.t[k - 1]);
            p.t[k - 1] = 0;
            if (o) p.t[max(k, v)] += l;
            return;
        }
    }
}
i64 f[maxn];
int v[maxn];
void solve()
{
    for (int j=0;j<4;++j)
    {
        int x;
        cin >> x;
        for (int i=1;i<=n;++i) if ((i & 3) == j) a[i] = min(k + 1, b[i] ^ x);
    }
    for (int i=1;i<=n;++i) st[0][i] = i;
    for (int i=1;i<20;++i)
        for (int j=1;j+(1<<i)-1<=n;j+=(1<<i))
        {
            int x = st[i - 1][j], y = st[i - 1][j + (1 << i - 1)];
            if (s[i][(j - 1) >> i] == '1') st[i][j] = a[y] >= i ? y : x;
            else st[i][j] = a[x] >= i ? x : y;
        }
    i64 ans = 0;
    f[1] = 1;
    for (int i=2;i<=m;++i) if (v[i])
    {
        int x = 1;
        while ((1 << x) < i) ++x;
        node p;
        ask(1, i, x, p);
        i64 s = p.m;
        for (int j=0;j<maxm;++j) s += p.t[j];
        f[i] = s;
    }
    for (int i=1;i<=m;++i) ans ^= f[c[i]] * i;
    cout << ans << '\n';
}
int main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for (int i=1;i<=n;++i) cin >> b[i];
    for (int i=1;i<=m;++i) cin >> c[i], v[c[i]] = 1;
    int lim = 1;
    while (lim < n) lim <<= 1, ++k;
    for (int i=1;i<=k;++i) cin >> s[i];
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}